﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UtilityExtensions;
using BOL;
using BOL.Ventas;
using DAL;
using System.Data.SqlClient;
using Website.Logger;

namespace UILGestion.VentaAdmin.OrdenVenta
{
    public partial class OrdenVentaList2Grid : System.Web.UI.UserControl
    {


        public OrdenVentaList2Grid()
        {
            bol = new BOL.Ventas.OrdenVentaBL();
            _db = new dbGestionDataContext(DbUtil.strConnString);
        }
        private BOL.Ventas.OrdenVentaBL bol;
        private dbGestionDataContext _db;



        public event EventHandler DataBound;

        public int pagesize { get { return Util.GetPageSizeCookie(); } }
        protected void Page_Load(object sender, EventArgs e)
        {
            pager.PageSize = pagesize;

        }

        public override void DataBind()
        {
        ListView1.DataBind();
        }
        public string DataSourceID
        {
            set { ListView1.DataSourceID = value; }
        }
        public object DataSource
        {
            set { ListView1.DataSource = value; }
        }

        protected void ListView1_DataBound(object sender, EventArgs e)
        {
            if (DataBound != null)
                DataBound(sender, e);
        }
        public DataPager DataPager
        {
            get { return pager; }
        }
        public int TopRolId
        {
            get { return (int)ListView1.DataKeys[0].Value; }
        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var link = e.Item.FindControl("HyperLink1") as HyperLink;
                var r = e.Item as ListViewDataItem;
                var d = r.DataItem as OrdenCompraListInfo;
                //link.NavigateUrl ="javascript:PageMethods.ToggleTag({0},'{1}',ToggleTagCallback)".Fmt(d.idInternal, link.ClientID);

              
            }
        }
        protected void Deleteitem(object sender, EventArgs e)
        {
            try
            {

                ImageButton lnkRemove = (ImageButton)sender;
                bol.DeleteOrdenVenta(lnkRemove.CommandArgument.ToInt());
                this.DataBind();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('"+sqlex.Message.Replace("'","").Replace("\r","").Replace("\n","")+"');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }

        protected void opcionBTN(object sender, EventArgs e)
        {
            try
            {

                Button lnk = (Button)sender;
                 
                 
                       
                 
                if (lnk.ID == "OkMinButton")
                {
                                    

                    bol.UpdateDatosMinuta(lnk.CommandArgument.ToInt(),
                                   ((TextBox)lnk.Parent.FindControl("minFechaPrintTXT")).Text.ToDate(),
                                    ((TextBox)lnk.Parent.FindControl("minFechaFirmClienteTXT")).Text.ToDate(),
                                     ((TextBox)lnk.Parent.FindControl("minfechaFirmEmpresaTXT")).Text.ToDate(),
                                      ((TextBox)lnk.Parent.FindControl("minfechaFirmBancoTXT")).Text.ToDate() );
                }
                if (lnk.ID == "OkEnvioBTN")
                {
                    bol.UpdateDatosEnvio(lnk.CommandArgument.ToInt(),
                                   ((TextBox)lnk.Parent.FindControl("envioNotariaTXT")).Text.ToDate(),
                                    ((TextBox)lnk.Parent.FindControl("envioBancoTXT")).Text.ToDate(),
                                     ((TextBox)lnk.Parent.FindControl("envioCofideTXT")).Text.ToDate(),
                                      ((TextBox)lnk.Parent.FindControl("envioFMVTXT")).Text.ToDate());
                }
                if (lnk.ID == "OkEscrituraBTN")
                {
                    bol.UpdateDatosEscritura(lnk.CommandArgument.ToInt(),
                                   ((TextBox)lnk.Parent.FindControl("escriturakardexTXT")).Text.ToDate(),
                                    ((TextBox)lnk.Parent.FindControl("escrituraKardexNumTXT")).Text,
                                     ((TextBox)lnk.Parent.FindControl("escrituraPrintTXT")).Text.ToDate(),
                                      ((TextBox)lnk.Parent.FindControl("escrituraFirmClienteTXT")).Text.ToDate(),
                                     ((TextBox)lnk.Parent.FindControl("escrituraFirmEmpresaTXT")).Text.ToDate(),
                                      ((TextBox)lnk.Parent.FindControl("escrituraFirmBancoTXT")).Text.ToDate()

                                      );
                }
                if (lnk.ID == "OKLegalBTN")
                {
                    bol.UpdateDatosLegal(lnk.CommandArgument.ToInt(),
                                   ((TextBox)lnk.Parent.FindControl("legalInsClienteTXT")).Text.ToDate(),
                                    ((TextBox)lnk.Parent.FindControl("legalEntregaTXT")).Text.ToDate(),
                                     ((TextBox)lnk.Parent.FindControl("legalHipotOtroBancoTXT")).Text.ToDate());
                }

               
                this.DataBind();

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('"+sqlex.Message.Replace("'","").Replace("\r","").Replace("\n","")+"');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }
        

        
    }
}